/**
 * @author 李亚松
 * @email  lysong01@gmail.com
 * 
 * WangyiBlogCrawler.java 2012-12-2
 * description 
 */
package liyasong.tool.crawler;

import liyasong.util.RegexUtil;

import org.htmlparser.NodeFilter;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.HasParentFilter;
import org.htmlparser.filters.TagNameFilter;

public class WangyiBlogCrawler extends AbstractCrawler {

	public WangyiBlogCrawler(String blogSite) {
		super(blogSite);
	}

	public WangyiBlogCrawler(String blogSite, String charset) {
		super(blogSite, charset);
	}

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		String site = "http://firecacada.blog.163.com/";
		WangyiBlogCrawler crawler = new WangyiBlogCrawler(site, "gbk");
		crawler.crawl();
		crawler.resultWrite("resources");
		System.out.println(crawler.getArticles().size());
	}
	
	@Override
	protected NodeFilter firstLinkFilter() {
		NodeFilter parent = new AndFilter(new TagNameFilter("h3"), 
				new HasAttributeFilter("class", "title thide bdc0 mtag"));
		NodeFilter nodeFilter = new AndFilter(new TagNameFilter("a"), 
				new HasParentFilter(parent));
		return nodeFilter;
	}

	@Override
	protected NodeFilter lastLinkFilter() {
		NodeFilter parent = new AndFilter(new TagNameFilter("div"), 
				new HasAttributeFilter("class", "pleft thide"));
		NodeFilter nodeFilter = new AndFilter(new TagNameFilter("a"), 
				new HasParentFilter(parent));
		return nodeFilter;
	}

	@Override
	protected NodeFilter titleFilter() {
		NodeFilter parent = new AndFilter(new TagNameFilter("h3"), 
				new HasAttributeFilter("class", "title pre fs1"));
		NodeFilter nodeFilter = new AndFilter(new TagNameFilter("span"), 
				new HasParentFilter(parent));
		return nodeFilter;
	}

	@Override
	protected NodeFilter authorFilter() {
		NodeFilter nodeFilter = new AndFilter(new TagNameFilter("a"), 
				new HasAttributeFilter("class", "fc05 m2a nicktag"));
		return nodeFilter;
	}

	@Override
	protected NodeFilter descriptionFilter() {
		NodeFilter self = new AndFilter(new TagNameFilter("div"), 
				new HasAttributeFilter("class", "bct fc05 fc11 nbw-blog ztag"));
 		NodeFilter parent = new AndFilter(new TagNameFilter("div"), 
				new HasAttributeFilter("class", "nbw-bitm bdwb bds2 bdc0 "));
		NodeFilter nodeFilter = new AndFilter(self, 
				new HasParentFilter(parent));
		return nodeFilter;
	}

	@Override
	protected NodeFilter pubDateFilter() {
		NodeFilter self = new AndFilter(new TagNameFilter("span"), 
				new HasAttributeFilter("class", "blogsep"));
 		NodeFilter parent = new AndFilter(new TagNameFilter("span"), 
				new HasAttributeFilter("class", "pleft"));
		NodeFilter nodeFilter = new AndFilter(self, 
				new HasParentFilter(parent));
		return nodeFilter;
	}

	@Override
	protected String cleanDescription(String description) {
		return RegexUtil.cleanSpace(RegexUtil.cleanHtml(description));
	}

	@Override
	protected String cleanTitle(String title) {
		return title;
	}

	@Override
	protected String cleanAuthor(String author) {
		return RegexUtil.cleanSpace(author);
	}

}
